package cn.micro.keep.accounts.captcha;



import cn.micro.keep.accounts.common.component.RedisService;
import cn.micro.keep.accounts.common.utils.RedisKeyConstant;
import cn.micro.keep.accounts.common.utils.uuid.IdUtils;
import cn.micro.keep.accounts.config.CaptchaConfig;
import cn.micro.keep.accounts.entity.vo.CaptchaResult;
import com.wf.captcha.base.Captcha;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * EasyCaptcha 业务类
 *
 *
 */
@Component
@RequiredArgsConstructor
public class EasyCaptchaService {

    private final EasyCaptchaProducer easyCaptchaProducer;

//    private final RedisTemplate redisTemplate;

    @Resource
    private RedisService redisService;

    private final CaptchaConfig captchaConfig;

    /**
     * 获取验证码
     *
     * @return
     */
    public CaptchaResult getCaptcha() {
        // 获取验证码
        Captcha captcha = easyCaptchaProducer.getCaptcha();
        String captchaText = captcha.text(); // 验证码文本
        String captchaBase64 = captcha.toBase64(); // 验证码图片Base64字符串
        // 验证码文本缓存至Redis，用于登录校验
        String verifyCodeKey = IdUtils.simpleUUID();
        redisService.set(RedisKeyConstant.VERIFY_CODE_CACHE_PREFIX + verifyCodeKey,captchaText,captchaConfig.getTtl());
        return CaptchaResult.builder()
                .verifyCodeKey(verifyCodeKey)
               .verifyCodeBase64(captchaBase64)
                .build();
    }

}
